package com.jpx.jxcupms.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.jxcupms.entity.TMenu;
import com.jpx.jxcupms.entity.TRole;
import com.jpx.jxcupms.entity.TRoleMenu;
import com.jpx.jxcupms.service.ITRoleMenuService;
import com.jpx.jxcupms.service.ITRoleService;
import com.jpx.jxc.common.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/upms/tRole")
@Slf4j
public class TRoleController {
    @Autowired
    ITRoleService itRoleService;
    @Autowired
    ITRoleMenuService itRoleMenuService;

    @RequestMapping("/roleMenuList")
    public ResultEntity roleMenuList(TRole tRole){
        Wrapper<TRole> wrapper = new EntityWrapper<TRole>();
        wrapper.like("name",tRole.getName());
        List<TRole> tRoles = itRoleService.selectList(wrapper);
        return ResultEntity.ok(tRoles);
    }

    @RequestMapping("/getMenuByRoleid")
    public ResultEntity getMenuByRoleid(Integer id){
        List<TMenu> tMenus = itRoleService.getMenuByRoleid(id);
        List<Integer> list = new ArrayList<Integer>();
        for (TMenu tMenu:tMenus) {
            list.add(tMenu.getId());
        }
        return ResultEntity.ok(list);
    }

    @RequestMapping("/getRoleById")
    public ResultEntity getRoleById(Integer id){
        TRole tRole = itRoleService.selectById(id);
        return ResultEntity.ok(tRole);
    }

    @RequestMapping("/updateRole")
    public ResultEntity updateRole(TRole tRole){
        System.err.println(tRole);
        itRoleService.insertOrUpdate(tRole);
        return ResultEntity.ok();
    }

    @RequestMapping("/delRole")
    @Transactional
    public ResultEntity delRole(Integer roleid){
        Wrapper<TRoleMenu> wrapper = new EntityWrapper<TRoleMenu>();
        wrapper.eq("role_id",roleid);
        itRoleMenuService.delete(wrapper);
        itRoleService.deleteById(roleid);
        return ResultEntity.ok();
    }
}
